//金币模式匹配
var matchSuper = require("./matchSuper.js")
module.exports = function(manager) {
	matchSuper.call(this)
	this.manager = manager
	this.matchType = ""
	this.roomList = {} 					//房间内玩家列表
	this.playerCount = 0 				//玩家总数量
	this.notFullRoomMap = [] 			//未满人房间列表
	this.lastRoomId = 0 				//最后离开的房间
	this.lastLeaveUid = 0 				//最后离开的玩家
	this.gameMode = ""
	var local = {}
	this.data = {
		maxScore : 0,
		minScore :0,
		rate : 0,
		charge : 0,
		willWin : false,
		graded : false,
		robotMaxScore : 0,
		robotMinScore : 0,
		matchType : "goldMatch"
	}
	//改变总玩家人数
	this.changePlayerCount = function(number) {
		console.log("changePlayerCount")
		if(number && typeof(number) == "number"){
			this.playerCount += number
		}
		console.log("number :"+number)
		console.log("this.playerCount : " + this.playerCount)
	}
	//匹配成功
	this.matchSuccess = function(players) {
		console.log("matchSuccess")
		console.log(players)
		console.log(this.gameMode)
		var level = 0
		for(var i = 0;i < players.length;i++){
			if(players[i].level && players[i].level > level){
				level = players[i].level
			}
		}
		this.manager.matchSuccess(this.gameMode,this.matchType,players,this.data)
	}
	//匹配信息
	this.setInfo = function(info) {
		for(var i in this.data){
			if(info[i]){
				this.data[i] = info[i]
			}
		}
		this.matchType = info.matchType
	}
	//加入匹配
	this.joinMatching = function(player,info) {
		if(player.uid < 10000){
			player.gold = Math.floor(Math.random() * (this.data.robotMaxScore - this.data.robotMinScore)) + this.data.robotMinScore
		}
		if(player.gold > this.data.maxScore || player.gold < this.data.minScore){
			return false
		}
		if(this.data.willWin && player.goldPlayCount >= 20){
			// console.log("goldPlayCount : ",goldPlayCount)
			return false
		}
		this.matchList[player.uid] = player
		return true
	}
	//创建匹配房间成功
	this.matchRoomBegin = function(roomId,players){
		this.roomList[roomId] = players
		if(this.roomList[roomId].length < this.expectCount){
			this.notFullRoomMap.push(roomId)
		}
		this.changePlayerCount(players.length)
	}
	//匹配房间结束
	this.matchRoomOver = function(roomId,info) {
		if(this.roomList[roomId]){
			this.changePlayerCount(-this.roomList[roomId].length)
			if(this.roomList[roomId].length < this.expectCount){
				for(var i = 0;i < this.notFullRoomMap.length;i++){
					if(this.notFullRoomMap[i] == roomId){
						this.notFullRoomMap.splice(i,1)
					}
				}
			}
			delete this.roomList[roomId]
		}
	}
	//玩家退出
	this.userQuit = function(roomId,uid) {
		if(this.roomList[roomId]){
			this.lastRoomId = roomId
			this.lastLeaveUid = uid
			for(var i = 0;i < this.roomList[roomId].length;i++){
				if(this.roomList[roomId][i].uid == uid){
					if(this.roomList[roomId].length == this.expectCount){
						this.notFullRoomMap.push(roomId)
					}
					this.roomList[roomId].splice(i,1)
					this.changePlayerCount(-1)
					return
				}
			}
		}
	}
	//运行匹配
	this.beforeRunMatch = function() {
		//优先匹配旧房间
		var tmpList = []
		for(var i in this.matchList){
			tmpList.push(this.matchList[i])
		}
		if(tmpList.length == 0 && this.notFullRoomMap.length > 0){
			//有概率加入机器人
			var rand = Math.random()
			if(rand > 0.6){
				this.joinRobot()
			}
		}
		while(tmpList.length > 0 && this.notFullRoomMap.length > 0){
			var player = tmpList.splice(0,1)[0]
			var randIndex = Math.floor(Math.random() * this.notFullRoomMap.length)
			var roomId = parseInt(this.notFullRoomMap[randIndex])
			if(roomId != this.lastRoomId || player.uid != this.lastLeaveUid){
				this.joinMatchingRoom(roomId,player)
			}
		}
		this.runMatch()
	}
	this.joinMatchingRoom = function(roomId,player) {
		var uid = player.uid
		delete this.matchList[uid]
		this.roomList[roomId].push(player)
		if(this.roomList[roomId].length == this.expectCount){
			for(var i = 0;i < this.notFullRoomMap.length;i++){
				if(this.notFullRoomMap[i] == roomId){
					this.notFullRoomMap.splice(i,1)
				}
			}
		}
		var fun = function(flag,msg) {
			if(flag){
				this.changePlayerCount(1)
			}else{
				console.log("加入旧房间失败 : "+msg)
				//重新加入匹配
				this.matchList[player.uid] = player
				if(this.roomList[roomId].length == this.expectCount){
					this.notFullRoomMap.push(roomId)
				}
				for(var i = 0;i < this.roomList[roomId].length;i++){
					if(this.roomList[roomId][i].uid == uid){
						this.roomList[roomId].splice(i,1)
					}
				}
			}
		}
		this.manager.remote.mathchJoin(roomId,player,fun.bind(this))
	}
}